package zy.ejb;

import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class EJB3Factory {
	public static Object getEJB_Local(String beanName, Class serviceClass) {

		Object retService = null;
		Hashtable jndiProperties = new Hashtable();
		jndiProperties.put(Context.URL_PKG_PREFIXES,
				"org.jboss.ejb.client.naming");
		Context context;
		try {
			context = new InitialContext(jndiProperties);

			if (serviceClass.getName().endsWith("Local")) {
				// java:global/ear1/ejb1/Hello!zy.ejb.HelloLocal
				// java:app/ejb1/Hello!zy.ejb.HelloLocal
				// java:module/Hello!zy.ejb.HelloLocal
				// java:global/ear1/ejb1/Hello!zy.ejb.HelloRemote
				// java:app/ejb1/Hello!zy.ejb.HelloRemote
				// java:module/Hello!zy.ejb.HelloRemote
				// java:jboss/exported/ear1/ejb1/Hello!zy.ejb.HelloRemote
				Object obj1 = context
						.lookup("java:global/ear1/ejb1/Hello!zy.ejb.HelloLocal");
				Object obj2 = context
						.lookup("java:app/ejb1/Hello!zy.ejb.HelloLocal");
				Object obj3 = context
						.lookup("java:module/Hello!zy.ejb.HelloLocal");

				Object obj4 = context
						.lookup("java:global/ear1/ejb1/Hello!zy.ejb.HelloRemote");
				Object obj5 = context
						.lookup("java:app/ejb1/Hello!zy.ejb.HelloRemote");
				Object obj6 = context
						.lookup("java:module/Hello!zy.ejb.HelloRemote");
				Object obj7 = context
						.lookup("java:jboss/exported/ear1/ejb1/Hello!zy.ejb.HelloRemote");

				return context.lookup("java:module/Hello!zy.ejb.HelloLocal");
			}

			String appName = "ear1";
			String moduleName = "ejb1";// 工程名
			String distinctName = "";
			String jndi = "ejb:" + appName + "/" + moduleName + "/"
					+ distinctName + "/" + beanName + "!"
					+ serviceClass.getName();

			retService = context.lookup(jndi);

			return retService;
		} catch (NamingException ex) {
			ex.printStackTrace();
			return null;
		}
	}
}
